<?php
// +----------------------------------------------------------------------
// | ZGUAN [ 致力于帮助更多的中小企业物联网转型 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2024 https://www.zguan.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ZGUAN 并不是自由软件，未经许可不能去掉ZGUAN 相关版权
// +----------------------------------------------------------------------
// | Author: ZGUAN Team <admin@zguan.cn>  0769-82282996  15899929162
// +----------------------------------------------------------------------
namespace app\common\dao\article\guide;

use app\common\dao\BaseDao;
use app\common\model\article\guide\GuideModel;

class GuideDao extends BaseDao
{

    /**
     * @return GuideModel
     */
    protected function getModel(): string
    {
        return GuideModel::class;
    }

    public function search(array $where, int $propertyId = null)
    {
        return $this->getModel()::getDB()
            ->when($propertyId !== null, function ($query) use ($propertyId) {
                $query->where('property_id', $propertyId);
            })
            ->when(isset($where['cate_id']) && $where['cate_id'] !== '', function ($query) use ($where) {
                $query->where('cate_id', intval($where['cate_id']));
            })
            ->when(isset($where['is_show']) && $where['is_show'] !== '', function ($query) use ($where) {
                $query->where('is_show', intval($where['is_show']));
            })
            ->when(isset($where['is_top']) && $where['is_top'] !== '', function ($query) use ($where) {
                $query->where('is_top', intval($where['is_top']));
            })
            ->when(isset($where['keywords']) && $where['keywords'] !== '', function ($query) use ($where) {
                $query->whereLike('title|keywords|desc', '%' . trim($where['keywords']) . '%');
            });
    }


    /**
     * 修改状态
     *
     * @param int $id
     * @param int $status
     * @return int
     * @throws \think\db\exception\DbException
     */
    public function switchStatus(int $id, int $status)
    {
        return ($this->getModel()::getDB())->where($this->getPk(), $id)->update([
            'is_show' => $status
        ]);
    }

    public function placedTop(int $id, int $placedTop)
    {
        return ($this->getModel()::getDB())->where($this->getPk(), $id)->update([
            'is_top' => $placedTop
        ]);
    }

}